import { IPlugin, YFWRequestOptions } from './types';
import AxiosRequest from "./axiosRequest";

export default function YFWRequestImpl(options: YFWRequestOptions, plugins: IPlugin[] = []) {
    const initState = plugins.reduce((res, plugin) => {
        const state = plugin.init?.(options)
        if (state) {
            return {
                ...res,
                ...state
            }
        }
        return res
    }, {})
    const initOptions = {
        ...options,
        ...initState
    }
    const requestIns = new AxiosRequest(initOptions)
    requestIns.pluginsImpl = plugins.map(p => p(requestIns, options))
    
    return requestIns
}